/*
 * TypStartovniKategorieDialog.java
 *
 * Created on 7. leden 2007, 17:04
 */

package cz.results.winter6.view.dialogs.stkategorie;

import cz.results.winter6.io.CSV2XML;
import cz.results.winter6.io.IoConfig;
import cz.results.winter6.model.KategorieConfigList;
import java.io.File;
import java.util.TreeMap;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;


public class TypStartovniKategorieDialog extends JDialog implements WantsValidationStatus {
    
    private TreeMap<String,KategorieConfigList> startovniKategorieConfigMap;
    private String nazevTypuKategorie;
    /** vytvoreni noveho zaznamu, nejedna se o editaci uz drive vytvoreneho */
    private boolean novyZaznam;
    
    public TypStartovniKategorieDialog(JDialog parent, boolean modal, String nazevTypuKategorie, TreeMap<String,KategorieConfigList> startovniKategorieConfigMap, boolean novyZaznam) {
        super(parent, modal);
        
        setNazevTypuKategorie(nazevTypuKategorie);
        setStartovniKategorieConfigMap(startovniKategorieConfigMap);
        setNovyZaznam(novyZaznam);
        
        initComponents();
        
        jTextFieldNazev.setInputVerifier(new NazevTypuStartovniListinyValidator(this, jTextFieldNazev,"nemuze byt prazdny", getNazevTypuKategorie(), getStartovniKategorieConfigMap()));
        
        if(!isNovyZaznam()){
            jTextFieldNazev.setText(getNazevTypuKategorie());
            jTextFieldNazevPrint.setText(getKategorieConfigList().getNazev());
            jTextFieldPocetJizd.setText(String.valueOf(getKategorieConfigList().getPocetJizd()));
            jTextFieldXSLFile.setText(getKategorieConfigList().getXslFilename());
        }
    }
    
    public void validateFailed(){
        jButtonOk.setEnabled(false);  // Called when a component has failed validation.
        
    }
    public void validatePassed(){
        jButtonOk.setEnabled(true);;  // Called when a component has passed validation.
    }
    
    public KategorieConfigList getKategorieConfigList(){
        return getStartovniKategorieConfigMap().get(getNazevTypuKategorie());
    }
    
    public String getNazevTypuKategorie() {
        return nazevTypuKategorie;
    }
    
    public void setNazevTypuKategorie(String nazevTypuKategorie) {
        this.nazevTypuKategorie = nazevTypuKategorie;
    }
    
    public TreeMap<String, KategorieConfigList> getStartovniKategorieConfigMap() {
        return startovniKategorieConfigMap;
    }
    
    public void setStartovniKategorieConfigMap(TreeMap<String, KategorieConfigList> startovniKategorieConfigMap) {
        this.startovniKategorieConfigMap = startovniKategorieConfigMap;
    }
    
    public boolean isNovyZaznam() {
        return novyZaznam;
    }
    
    public void setNovyZaznam(boolean novyZaznam) {
        this.novyZaznam = novyZaznam;
    }
    
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jTextFieldNazev = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldPocetJizd = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldXSLFile = new javax.swing.JTextField();
        jButtonFileChooser = new javax.swing.JButton();
        jButtonOk = new javax.swing.JButton();
        jButtonZrus = new javax.swing.JButton();
        jTextFieldNazevPrint = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();

        setLocationByPlatform(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jLabel1.setText("n\u00e1zev - index");

        jTextFieldNazev.setNextFocusableComponent(jTextFieldPocetJizd);

        jLabel2.setText("po\u010det j\u00edzd");

        jTextFieldPocetJizd.setNextFocusableComponent(jTextFieldXSLFile);

        jLabel3.setText("xsl file");

        jTextFieldXSLFile.setNextFocusableComponent(jButtonFileChooser);

        jButtonFileChooser.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/fileopen.png")));
        jButtonFileChooser.setText("file");
        jButtonFileChooser.setNextFocusableComponent(jButtonOk);
        jButtonFileChooser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonFileChooserActionPerformed(evt);
            }
        });

        jButtonOk.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/button_ok.png")));
        jButtonOk.setText("ok");
        jButtonOk.setNextFocusableComponent(jButtonZrus);
        jButtonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOkActionPerformed(evt);
            }
        });

        jButtonZrus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/button_cancel.png")));
        jButtonZrus.setText("zru\u0161");
        jButtonZrus.setNextFocusableComponent(jTextFieldNazev);
        jButtonZrus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonZrusActionPerformed(evt);
            }
        });

        jLabel4.setText("n\u00e1zev na startovku");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldXSLFile, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                            .addComponent(jTextFieldNazev, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                            .addComponent(jTextFieldPocetJizd, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldNazevPrint, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(96, 96, 96)
                        .addComponent(jButtonOk, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonZrus, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(24, 24, 24)
                .addComponent(jButtonFileChooser)
                .addGap(47, 47, 47))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldNazev, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldNazevPrint, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldPocetJizd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldXSLFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jButtonFileChooser))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonZrus)
                    .addComponent(jButtonOk))
                .addGap(27, 27, 27))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void jButtonFileChooserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFileChooserActionPerformed
        final JFileChooser fc = new JFileChooser(IoConfig.dataPath);
        fc.addChoosableFileFilter(new FileNameExtensionFilter("XSL file", "xsl"));
        
        int returnVal = fc.showOpenDialog(this);
        
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            jTextFieldXSLFile.setText(CSV2XML.najdiStr("(.*)\\.xsl",file.getName()));
        }
    }//GEN-LAST:event_jButtonFileChooserActionPerformed
    
    private void jButtonZrusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonZrusActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event_jButtonZrusActionPerformed
    
    private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOkActionPerformed
        
        
        if(isNovyZaznam()){

            setNazevTypuKategorie(jTextFieldNazev.getText().trim());
            getStartovniKategorieConfigMap().put(getNazevTypuKategorie(), new KategorieConfigList());
            
        } else {
            
            //zmena jmena kategorie - jiny rezim protoze nazev kategorie je klic do mapy kategorieConfigListu
            if(!jTextFieldNazev.getText().trim().equals(getNazevTypuKategorie())){  //jestlize byl zmenen nazev
                getStartovniKategorieConfigMap().put(jTextFieldNazev.getText().trim(), getKategorieConfigList());  //vlozim stary kategorieConfigList pod novym nazvem
                getStartovniKategorieConfigMap().remove(getNazevTypuKategorie());               //smazu puvodni
                setNazevTypuKategorie(jTextFieldNazev.getText().trim());
            }
        }
        
        getKategorieConfigList().setNazev(jTextFieldNazevPrint.getText().trim());
        getKategorieConfigList().setPocetJizd(Integer.valueOf(jTextFieldPocetJizd.getText().trim()));
        getKategorieConfigList().setXslFilename(jTextFieldXSLFile.getText().trim());
        
        setVisible(false);
        dispose();
    }//GEN-LAST:event_jButtonOkActionPerformed
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonFileChooser;
    private javax.swing.JButton jButtonOk;
    private javax.swing.JButton jButtonZrus;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextFieldNazev;
    private javax.swing.JTextField jTextFieldNazevPrint;
    private javax.swing.JTextField jTextFieldPocetJizd;
    private javax.swing.JTextField jTextFieldXSLFile;
    // End of variables declaration//GEN-END:variables
    
}
